//
//  LanguageManager.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/21.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Localize_Swift

/// 语言
enum LanguageTpye: String {
    case zh = "zh-Hans"
    case ru = "ru"
}

struct LanguageManager {
    
    /// 当前账户是不是中国
    static var isCountryCN: Bool {
        UserProfile.country == 0
    }
    
    /// 本地当前语言是不是中文
    static var isLanguageZH: Bool {
        LanguageTpye(rawValue: localizeCurrentLanguage()) == .zh
    }
    
    /// 当前语言
    static var currentLanguage: LanguageTpye {
        LanguageTpye(rawValue: localizeCurrentLanguage()) ?? .zh
    }
    
    /// 设置当前语言
    /// - Parameter language: 语言类型
    static func setLanguage(_ language: LanguageTpye) {
        Localize.setCurrentLanguage(language.rawValue)
    }
    
    /// 设置当前语言
    /// - Parameter country: 国家 id
    static func setLanguage(_ country: Int) {
        let language = country == 0 ? "zh-Hans": "ru"
        Localize.setCurrentLanguage(language)
    }
    
    /// 根据当前用户的国家状态，返回需要显示的文本内容
    /// - Parameters:
    ///   - cn: 中文
    ///   - ru: 俄语
    /// - Returns: 返回需要显示的文本内容
    static func currentLanguageToText(_ cn: String?, _ ru: String?) -> String {
        LanguageManager.isLanguageZH ? (cn ?? ""): (ru ?? "")
    }
 
    
    // 获取项目设置的本地语言
    private static func localizeCurrentLanguage() -> String {
        let language = Localize.currentLanguage()
        if language.hasPrefix("zh") {
            return "zh-Hans"
        }
        return language
    }
    
}

extension LanguageManager {

    /// 根据国家 id，判断 2 个国家是不是同一种语言。
    static func isSameLanguage(_ a: Int, _ b: Int) -> Bool {
         let countryA = a == 0 ? 0 : 1
         let countryB = b == 0 ? 0 : 1
         return countryA == countryB
     }

}
